<template>
  <div class="home" ref="homeRef">
    <div class="nav-bar">
      <div class="title">携程旅行</div>
    </div>
    <div class="banner">
      <img src="@/assets/img/banner.png" alt="" style="width: 100%" />
    </div>
    <div class="location bottom-theme-line">
      <div class="position" @click="setLocation">
        {{ currentCity.cityName ? currentCity.cityName : '泉州' }}
      </div>
      <div class="icon" @click="getLocation">
        <span>我的位置</span>
        <img src="@/assets/img/location.png" alt="" />
      </div>
    </div>
    <div class="item data-range">
      <div class="start" @click="showCalendar = true">
        <div class="data">
          <span class="tip">入住</span>
          <span class="time">{{ startData }}</span>
        </div>
      </div>
      <div>
        共<span class="stay">{{ diffDay ? diffDay : 1 }}</span
        >晚
      </div>
      <div class="end">
        <div class="data">
          <span class="tip">离店</span>
          <span class="time">{{ endDate }}</span>
        </div>
      </div>
    </div>
    <div class="item price-people">
      <div class="price">价格不限</div>
      <div class="people">人数不限</div>
    </div>
    <div class="item keyword bottom-theme-line">关键字/位置/民宿名</div>
    <div class="item hot-suggest bottom-theme-line">
      <template v-for="(item, index) in hotSuggest" :key="index">
        <div
          class="item"
          :style="{ color: item.tagText.color, background: item.tagText.background.color }"
        >
          {{ item.tagText.text }}
        </div>
      </template>
    </div>
    <button class="search-btn" @click="goSearch">搜索</button>

    <homeCategory></homeCategory>

    <homeTuijian></homeTuijian>

    <van-calendar
      v-model:show="showCalendar"
      type="range"
      @confirm="onConfirm"
      show-confirm
      color="#d4237a"
    />
  </div>
  <van-search
    class="search"
    v-if="isSearchShow"
    placeholder="请输入搜索关键词"
    input-align="center"
    left-icon=""
    clearable
  >
    <template #left>
      <div style="align-items: center">
        <div>
          <span style="color: lightgray">住</span>
          <span>{{ searchStartDate }}</span>
        </div>
        <div>
          <span style="color: lightgray">离</span>
          <span>{{ searchEndDate }} </span>
        </div>
      </div>
    </template>
  </van-search>
</template>

<script setup name="Home">
import { useRouter } from 'vue-router'
import useCityStore from '@/stores/modules/city'
import { storeToRefs } from 'pinia'
import { formatData, getDiffDay, fomatMounth } from '@/utils/formatData'
import { computed, onActivated, onMounted, ref, watch } from 'vue'
import { getHotSuggest } from '@/services/modules/home'
import homeCategory from './cpns/home/home-category.vue'
import homeTuijian from './cpns/home/home-tuijian.vue'
import useScrollLoad from '@/hooks/useScroll'

const cityStore = useCityStore()
const { currentCity } = storeToRefs(cityStore)
const router = useRouter()
const currentDate = new Date()
const startData = ref(formatData(currentDate))
const searchStartDate = ref(fomatMounth(currentDate))
const endDate = ref(formatData(currentDate.setDate(currentDate.getDate() + 1)))
const searchEndDate = ref(fomatMounth(currentDate.setDate(currentDate.getDate() + 1)))
const showCalendar = ref(false)
const diffDay = computed(() => {
  return getDiffDay(startData.value, endDate.value)
})
const hotSuggest = ref([])
const homeRef = ref()
onMounted(() => {
  getHotSuggests()
})
const setLocation = () => {
  // navigator.geolocation.getCurrentPosition(
  //   (position) => {
  //     console.log(position)
  //   },
  //   (err) => {
  //     console.log(err)
  //   },
  // )
  router.push('/city')
}
const getLocation = () => {
  navigator.geolocation.getCurrentPosition(
    (position) => {
      console.log(position)
    },
    (err) => {
      console.log(err)
    },
  )
}
const onConfirm = (value) => {
  const selectStartDate = value[0]
  const selectEndDate = value[1]
  startData.value = formatData(selectStartDate)
  endDate.value = formatData(selectEndDate)
  showCalendar.value = false
}
const getHotSuggests = async () => {
  const result = await getHotSuggest()
  hotSuggest.value = result.data
}
const goSearch = () => {
  router.push({
    path: '/search',
    query: {
      startDate: startData.value,
      endDate: endDate.value,
      currentCity: currentCity.cityName ? currentCity.cityName : '泉州',
    },
  })
}
const { isLoading, isSearch, scrollTop } = useScrollLoad(homeRef)

const isSearchShow = computed(() => {
  return scrollTop.value >= 720
})

onActivated(() => {
  homeRef.value.scrollTo({
    top: scrollTop.value,
  })
})
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100vh;
  box-sizing: border-box;
  overflow-y: auto;
  padding-bottom: 50px;
  .nav-bar {
    height: 50px;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    .title {
      font-size: 20px;
      font-weight: bold;
      color: var(--primary-color);
    }
  }
  .position {
    margin-left: 20px;
  }
  .location {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    .icon {
      display: flex;
      line-height: 24px;
    }
    .icon > img {
      width: 24px;
      height: 24px;
      margin-left: 10px;
    }
  }
  .item {
    display: flex;
    justify-content: space-between;
    padding: 20px;
    align-items: center;
  }
  .data {
    display: flex;
    flex-direction: column;
    .tip {
      margin-bottom: 10px;
      color: var(--primary-color);
    }
  }
  .stay {
    color: var(--primary-color);
    margin: 0px 10px 0px 10px;
  }
  .price-people {
    color: lightgray;
    opacity: 0.8;
    font-size: 14px;
  }
  .keyword {
    color: lightgray;
    opacity: 0.8;
  }
  .hot-suggest {
    color: var(--primary-color);
    opacity: 0.5;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0px 40px 0px 40px;
    .item {
      padding: 3px;
      margin: 3px;
      border-radius: 5px;
      font-size: 14px;
    }
  }
  .search-btn {
    color: #fff;
    width: 80%;
    height: 38px;
    max-height: 50px;
    margin-left: 10%;
    border-radius: 20px;
    margin-top: 10px;
    background-image: var(--theme-liner-gradient);
  }
}
.search {
  width: 100%;
  display: flex;
  position: fixed;
  top: 0;
  height: 54px;
}
</style>
